<template>
    <el-container>
        <el-header class="header-tabs">
            <el-tabs type="card" v-model="groupId" @tab-change="tabChange">
                <el-tab-pane label="一个月内新签" name="1">
                    <template #label>
                        <el-badge :value="headnumber.month" :max="9999" class="item">
                            <div style="width:85px">一个月内新签</div>
                        </el-badge>
                    </template>
                </el-tab-pane>
                <el-tab-pane label="全部" name="0">
                    <template #label>
                        <el-badge :value="headnumber.total" :max="9999" class="item">
                            <div style="width:40px">全部</div>
                        </el-badge>
                    </template>
                </el-tab-pane>
            </el-tabs>
        </el-header>
        <el-header style="height: 50px;border-bottom: none">
            <div class="left-panel">
                <el-button type="primary" icon="el-icon-plus" @click="add" v-auth="'project.add'"></el-button>
                <el-button type="danger" plain icon="el-icon-delete" :disabled="selection.length == 0" @click="batch_del" v-auth="'project.delete'"></el-button>
                <el-button type="primary" @click="plExport" v-auth="'project.import'">批量导入</el-button>
                <el-button type="warning" @click="batch_export" v-auth="'project.export'">导出</el-button>
            </div>
        </el-header>
        <el-header>
            <div class="left-panel">
            </div>
            <div class="right-panel">
                <div class="right-panel-search">
                    <el-input style="margin:5px 10px" v-model="search.project_code" placeholder="请输入项目编号" clearable @keyup.enter="upsearch"></el-input>
                    <el-input style="margin:5px 10px" v-model="search.metting" placeholder="请输入会议名称" clearable @keyup.enter="upsearch"></el-input>
                    <el-input style="margin:5px 10px" v-model="search.hotel_name" placeholder="请输入酒店名称" clearable @keyup.enter="upsearch"></el-input>
                    <el-input style="margin:5px 10px" v-model="search.recommend_name" placeholder="请输入推荐人" clearable @keyup.enter="upsearch"></el-input>
                    <el-date-picker v-model="search.project_time" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 230px" />
                    <el-select v-model="search.type" placeholder="请选择类型" style="width:250px" clearable filterable multiple collapse-tags max-collapse-tags="2" collapse-tags-tooltip>
                        <el-option v-for="item in typeList" :key="item.id" :label="item.title" :value="item.id"/>
                    </el-select>
                    <!-- <el-input style="margin:5px 10px" v-model="search.company_name" placeholder="请输入客户名称" clearable @keyup.enter="upsearch"></el-input>
                    <el-input style="margin:5px 10px" v-model="search.metting" placeholder="请输入会议名称" clearable @keyup.enter="upsearch"></el-input>
                    <el-date-picker v-model="search.project_time" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" style="width: 230px" />
                    <el-select v-model="search.type" placeholder="请选择类型" clearable filterable>
                        <el-option v-for="item in typeList" :key="item.id" :label="item.title" :value="item.id"/>
                    </el-select> -->
                    <el-button type="primary" icon="el-icon-search" @click="upsearch">搜索</el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh">重置</el-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" row-key="id" :apiObj="apiObj" @selection-change="selectionChange" remoteSort remoteFilter border @row-dblclick="table_show" :cell-style="cellStyle" :params="query">
                <!--  tableName="projectTable" :column="projectColumn" -->
                <el-table-column fixed type="selection" width="50" reserve-selection align="center"></el-table-column>
                <!-- <template #type="{row}">
                    <div>{{row.type == 1 ? '会议' : row.type == 2 ? '婚礼' : row.type == 3 ? '会议代收' : row.type == 4 ? '会议代收+自付' : '赞助'}}</div>
                </template>
                <template #customer="{row}">
                    <div>{{row.customer.customer_name}}</div>
                </template>
                <template #banquet="{row}">
                    <div>{{row.banquet.banquet_name}}</div>
                </template>
                <template #recommend="{row}">
                    <div>{{row.recommend.recommend_name}}</div>
                </template> -->
                <!-- <template #is_ticket="scope">
                    <el-switch v-model="scope.row.is_ticket" @change="changeSwitch($event, scope.row)" :loading="scope.row.$switch_status" :active-value="1" :inactive-value="0"></el-switch>
                </template> -->
                <el-table-column fixed label="项目编号" prop="project_code" align="center" width="130"></el-table-column>
                <el-table-column fixed label="客户名称" prop="customer.company_name" align="center" width="140"></el-table-column>
                <el-table-column fixed label="会议名称" prop="metting" align="center" width="140"></el-table-column>
                <el-table-column fixed label="酒店名称" prop="hotel_name" align="center" width="140"></el-table-column>
                <el-table-column label="项目日期" prop="project_time" align="center" width="110"></el-table-column>
                <el-table-column label="类型" prop="type" align="center" width="100">
                    <template #default="{row}">
                        <div>{{row.type == 1 ? '会议' : row.type == 2 ? '婚礼' : row.type == 3 ? '会议代收' : row.type == 4 ? '会议代收+自付' : '赞助'}}</div>
                    </template>
                </el-table-column>
                <el-table-column label="宴会厅" prop="banquet.banquet_name" align="center" width="120"></el-table-column>
                <el-table-column label="推介人" prop="recommend.recommend_name" align="center" width="110"></el-table-column>
                <el-table-column label="项目总价" prop="total_price" align="center" width="90"></el-table-column>
                <el-table-column label="成本总额" prop="cost_all" align="center" width="90"></el-table-column>
                <el-table-column label="利润" prop="profit" align="center" width="90"></el-table-column>
                <el-table-column label="利润率" prop="profit_margin" align="center" width="90"></el-table-column>
                <el-table-column label="是否开票" prop="is_ticket" align="center" width="90">
                    <template #default="scope">
                        <el-switch v-model="scope.row.is_ticket" @change="changeSwitch($event, scope.row)" :loading="scope.row.$switch_status" :active-value="1" :inactive-value="0"></el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="备注" prop="remark" align="center" width="120"></el-table-column>
                <el-table-column label="操作" fixed="right" align="center" width="120">
                    <template #default="scope">
                        <el-button-group>
                            <el-button text type="primary" size="small" @click="table_edit(scope.row, scope.$index)" v-auth="'project.edit'">编辑</el-button>
                            <el-popconfirm title="确定删除吗？" @confirm="table_del(scope.row, scope.$index)">
                                <template #reference>
                                    <el-button text type="danger" size="small" v-auth="'project.delete'">删除</el-button>
                                </template>
                            </el-popconfirm>
                        </el-button-group>
                    </template>
                </el-table-column>
            </scTable>
        </el-main>
    </el-container>
	<save-dialog v-if="dialog.save" ref="saveDialog" @success="handleSuccess" @closed="dialog.save = false" draggable></save-dialog>
    <details-dialog v-if="dialog.details" ref="detailsDialog" @success="handleSuccess" @closed="dialog.details = false" :query="{project_id:this.project_id}"></details-dialog>
    <expoet-all v-if="dialog.expoet" ref="expoetDialog" @success="handleSuccess" @closed="dialog.expoet = false"></expoet-all>
</template>

<script>
import saveDialog from './save.vue'
import detailsDialog from './details.vue'
import ExpoetAll from '@/views/expoetAll.vue'

export default {
	name: '客户管理',
	components: {
		saveDialog,detailsDialog,ExpoetAll,
	},
	data() {
		return {
			dialog: {
				save: false,
				details: false,
				expoet: false,
			},
			apiObj: this.$API.project.project.list,
			selection: [],
			search: {
				project_code: null,
				metting: null,
				project_time: null,
				type: null,
				company_name: null,
                month: 1
			},
            groupId:"1",
            typeList:[
                {id:1,title:"会议"},
                {id:2,title:"婚礼"},
                {id:3,title:"会议代收"},
                {id:4,title:"会议代收+自付"},
                {id:5,title:"赞助"},
            ],
            project_id:"",
            headnumber:{},
            query:{
                month: 1
            },
            projectColumn: [
                {label: "项目编号",prop: "project_code"},
                {label: "客户名称",prop: "customer"},
                {label: "会议名称",prop: "metting"},
                {label: "酒店名称",prop: "hotel_name"},
                {label: "项目日期",prop: "project_time"},
                {label: "类型",prop: "type"},
                {label: "宴会厅",prop: "banquet"},
                {label: "推介人",prop: "recommend"},
                {label: "项目总价",prop: "total_price"},
                {label: "成本总额",prop: "cost_all"},
                {label: "利润",prop: "profit"},
                {label: "利润率",prop: "profit_margin"},
                // {label: "是否开票",prop: "is_ticket"},
                {label: "备注",prop: "remark"},
            ],
		}
	},
	mounted() {
        this.getNum()
	},
	methods: {
        
        cellStyle(row){
            let cellStyle = '';
            switch(row.row.type){
                case 3:
                    cellStyle = {color:'#000',backgroundColor:'#ff4a09'}
                    break;
                case 4:
                    cellStyle = {color:'#000',backgroundColor:'#f5971c'}
                    break;
                case 5:
                    cellStyle = {color:'#000',backgroundColor:'#67c23a'}
                    break;
                default:
                    cellStyle = {color:'#000',backgroundColor:'#ffffff'}
            }
            return cellStyle
        },
        success(res){
			if(res.code==1){
				this.$refs.table.refresh()
                this.getNum()
				this.$refs.importBox.close()
				this.$message.success("导入成功")
			} else{
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//添加
		add() {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('add')
			})
		},
		//编辑
		table_edit(row) {
			this.dialog.save = true
			this.$nextTick(() => {
				this.$refs.saveDialog.open('edit').setData(row)
			})
		},
		//查看
		table_show(row) {
            this.project_id = row.id
			this.dialog.details = true
			this.$nextTick(() => {
				this.$refs.detailsDialog.open('show').setData(row)
			})
		},
        plExport(){
            this.dialog.expoet = true
            this.$nextTick(() => {
                this.$refs.expoetDialog.open('project').setData('https://2171.mzth.cn/uploads/files/20240810/20240810161432c3ca99253.xlsx')
            })
        },
        // 导出
        async batch_export(){
            var id_all = [];
			this.selection.forEach((item) => {
				id_all.push(item.id)
			})
            let query = {
                // month: this.search.month,
                id_all:id_all,
                export:2,
                ...this.search
            }
            var res = await this.$API.project.project.list.get(query)
            if (res.code == 2) {
                const fileUrl = res.data.url;
                const link = document.createElement("a");
                link.href = fileUrl;
                link.download = fileUrl;
                link.click();
            } else {
                this.$alert(res.message, "提示", { type: "error" });
            }
        },
        // 切换开票状态
        async changeSwitch(val, row) {
			row.$switch_status = true;
            var res = await this.$API.project.project.update_status.post({
                is_ticket: row.is_ticket,
                id: row.id,
            })
			if (res.code == 1) {
				row.$switch_status = false;
				this.$refs.table.refresh();
				this.$message.success("操作成功");
			} else {
				this.$alert(res.message, "提示", { type: "error" });
			}
		},
		//删除
		async table_del(row, index) {
			var reqData = {id: row.id}
			var res = await this.$API.project.project.delete.post(reqData);
			if (res.code == 1) {
				//这里选择刷新整个表格 OR 插入/编辑现有表格数据
				this.$refs.table.tableData.splice(index, 1);
                this.$refs.table.refresh()
                this.getNum()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		async table_del_all(delete_ids) {
			var reqData = {id: delete_ids}
			var res = await this.$API.project.project.delete.post(reqData);
			if (res.code == 1) {
				this.$refs.table.refresh()
                this.getNum()
				this.$message.success("删除成功")
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		//批量删除
		async batch_del() {
			var delete_ids = [];
			this.selection.forEach((item) => {
				delete_ids.push(item.id)
			})
			this.$confirm(`确定删除选中的 ${this.selection.length} 项吗？`, '提示', {
				type: 'warning'
			}).then(() => {
				this.table_del_all(delete_ids)
			}).catch(() => {

			})
		},
		//表格选择后回调事件
		selectionChange(selection) {
			this.selection = selection;
		},
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		//本地更新数据
		handleSuccess(data, mode) {
			if (mode == 'add') {
				this.$refs.table.refresh()
			} else if (mode == 'edit') {
				this.$refs.table.refresh()
			} else if (mode == 'project') {
				this.$refs.table.refresh()
			}
            this.getNum()
		},
        refresh(){
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.groupId = "1"
            this.search.month = 1
            this.$refs.table.upData(this.search)
        },
        tabChange(e){
            this.groupId = e
            this.search.month = e
            this.upsearch()
        },
        async getNum(){
            var res = await this.$API.project.project.list.get();
            this.headnumber = res.data.extend
        },
	}
}
</script>

<style>
.titleBox {
	font-size: 14px;
	font-weight: 500;
	color: #0099ff;
	margin-left: 30px;
}
</style>
